package org.yray.sip.time;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;

import java.util.concurrent.PriorityBlockingQueue;

@Slf4j
public class SendPacketTimer implements Runnable {

    private PriorityBlockingQueue<BaseSendTask> priorityQueue=new PriorityBlockingQueue<>();



    @Override
    public void run() {

        for (;;)
        {
            try {
                BaseSendTask sendMediaPacket = priorityQueue.take();
                long currTime=System.currentTimeMillis();
                while (currTime<= sendMediaPacket.getTime())
                {
                    currTime=System.currentTimeMillis();
                }
                sendMediaPacket.doJob();
            } catch (Exception ex) {
                log.error("SendPacketTimer:{}", ExceptionUtils.getStackTrace(ex));
            }

        }

    }

    public void add(BaseSendTask baseSendTask)
    {
        priorityQueue.add(baseSendTask);
    }


}
